
function LoginCtrl($rootScope, $scope, $location, $http){

    if (globalAppParams.chatMode == 1){
        $location.path('/chat');
    }

    $scope.submitButton = new Object();
    initSubmitButton=function () {

        $scope.submitButton.label="התחברות";
        $scope.submitButton.disabled="";
    }
    disableSubmitButton=function () {
        $scope.submitButton.label="מתחבר...";
        $scope.submitButton.disabled="disabled";
    }
    initSubmitButton();


    $scope.login = function(){

        if ($scope.username == "" || $scope.password == ""){
            $(".login-alerts .alert-error").hide();
            $(".login-alerts .alert-error").removeClass("hidden");
            $(".login-alerts .alert-error").fadeIn();
            return;
        }
        disableSubmitButton();

        $http({method: 'GET', url: '/saftalk/getuser?email='+$scope.email+"&password="+$scope.password}).success(function(data, status, headers, config) {
            if (!data.success)
            {
                $(".login-alerts .alert-error").hide();
                $(".login-alerts .alert-error").removeClass("hidden");
                $(".login-alerts .alert-error").fadeIn();
                initSubmitButton();
                return;
            }

            globalAppParams.name=data.name;
            globalAppParams.userId=data.userId;
            if (data.photoId!=null){
                globalAppParams.photoId=data.photoId
            } else {
                globalAppParams.photoId='';
            }

            globalAppParams.channel=data.channel;
            globalAppParams.chatMode=$scope.chatMode;

            var animateTime=2000;

            $scope.$apply(function() {
                $rootScope.$broadcast('initChat');
                $location.path('/chat');
            });

            $(".loginForm").fadeOut(500,function(){
                $(".loginLoading").hide();
                $(".loginLoading").removeClass("hidden");
                $(".loginLoading").fadeIn();

            });

        }).error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with status
                // code outside of the <200, 400) range
            });
    }


}

// LoginCtrl.$inject = ['$scope','$location','$http'];